<?php 
## include required files
/*******************************/
require_once '../includefiles.php';
require_once '../model/users.php';
require_once '../model/nab.php';
require_once '../model/membershipplan.php';
require_once '../model/moduleuser.php';
require_once ("../includes/classes/pagination_class.php");
require_once ("../includes/download.php");

## creating objects of required models
$userObj 		= new Model_Users();
$planObj 		= new Model_PLAN();
$nabObj = new Model_NAB();
$moduleObj=new Model_ModuleUser();

$nab = $nabObj->getDetail();
/*******************************/
##Fetch all admin users from database
/******************************/

if($_GET['task'] == 'exportcsv')
{
	if($_REQUEST['paymentfor']!='0'){
	$paymentfor = addslashes(trim($_REQUEST['paymentfor']));
	} else {
		$paymentfor = '';
	}
	if($_REQUEST['id']!='')
	{
		$userId = base64_decode($_REQUEST['id']);
	}
	//$smarty->assign('searchindex',$searchindex);
	if($_REQUEST['from_date']!=''){
		$from_date = $_REQUEST['from_date'];
	} else {
		$from_date = '';
	}
	if($_REQUEST['to_date']!='To Date'){
		$to_date = $_REQUEST['to_date'];
	} else {
		$to_date = '';
	}
	$User = $userObj->getUserDetailsByUserId($userId);
	$historyArray  = $userObj->paymentHistoryAccordingToUserId($userId,$paymentfor,$from_date,$to_date,'','',false);	
	$filename = "Payment_history_for_".$User['first_name']."_".$User['last_name'].".csv";
	$data = array();
	$temp = array();
	foreach($historyArray[0] as $key=>$value)
	{
		if($key=="id")
			$key="Payment ID";
		$temp[] = $key	;
	}
	$data[] = $temp;
	
	$handler = fopen($filename,"w+");
	fputcsv($handler,$temp);
	foreach($historyArray as $row)
	{
		$temp = array();
		foreach($row as $key=>$val)
			$temp[] = $val;
		fputcsv($handler,$temp);	
	}

	
	fclose($handler);
	
	$downloader = new FileDownload();
	$downloader->do_download($filename,"");
	@unlink($filename);
	
}

if($_GET['q']=='paymenthisdetail')
{
	$moduelArray=$moduleObj->getModuleHelpText(26);
	$smarty->assign('moduelArray', $moduelArray);
}

if($_REQUEST['id']!='')
{
	$userId = base64_decode($_REQUEST['id']);
}
## for pagination
	$page = $_GET['page'];
	if(!isset($_GET['page']))
	$page = 1;
	else
	$page = $page;
	
## limit per pag 
$tetsimonialPerPage =10;
$StartRow = $tetsimonialPerPage  * ($page-1);
## Getting all leads from database according to user
## search value
if($_REQUEST['paymentfor']!='0'){
	$paymentfor = addslashes(trim($_REQUEST['paymentfor']));
} else {
	$paymentfor = '';
}
//$smarty->assign('searchindex',$searchindex);
if($_REQUEST['from_date']!=''){
	$from_date = $_REQUEST['from_date'];
} else {
	$from_date = '';
}
if($_REQUEST['to_date']!='To Date'){
	$to_date = $_REQUEST['to_date'];
} else {
	$to_date = '';
}
$historyArray  = $userObj->paymentHistoryAccordingToUserId($userId,$paymentfor,$from_date,$to_date,'','',false);
$orderitems = $userObj->getOrderItems($historyArray[0]["mem_id"]);
if(count($orderitems)>1)
	$smarty->assign('memcats', " And ".(count($orderitems)-1). " categories" );


$totalCount        = count($historyArray);
$smarty -> assign("recordsFound",$totalCount);
$result = $userObj->paymentHistoryAccordingToUserId($userId,$paymentfor,$from_date,$to_date,$tetsimonialPerPage,$StartRow,true);
for($i=0;$i<count($result); $i++)
{
	$result[$i]['amount'] = number_format( $result[$i]['amount'],2);
}


$show = 1;
$total_pages = ceil($totalCount / $tetsimonialPerPage);
	if($total_pages > 1)
	$smarty -> assign("showpgnation","yes");
	$showing    = !isset($_GET["id1"]) ? 1 : $page;
	
	## it shoould be file name 
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != ""  && $_GET[ 'page' ] > 1) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
		
	} 
	$rowsPerPage = ROW_PER_PAGE;
	$pageName = "home.php?q=paymenthisdetail&paymentfor=".$paymentfor."&from_date=".$from_date."&to_date=".$to_date."&id=".$_REQUEST['id'];
	if($total_pages!=0){
	$pg = new pagination();	
		$pagenation = $pg->pagination_admin($total_pages,$tetsimonialPerPage , $pageNum, $pageName, "", "");
	$smarty -> assign("pagenation", $pagenation);
	
	}
	//
	for($i=0;$i<count($result);$i++)
	{
		$getplandetail = $planObj->getPlanDetailsById($result[$i]['plan_id']);					
		if($getplandetail['name']!='')
		{
			$result[$i]['planname'] = $getplandetail['name'];
		}
		else
		{
			$result[$i]['planname'] = '-';
		}
		$currentDateTime = $result[$i]['status_date'];
      $result[$i]['status_date'] = date('h:i A', strtotime($currentDateTime));
	  $currentDateTime = explode(" ",$currentDateTime);
	  $result[$i]['status_date'] =$currentDateTime[0].' '.$result[$i]['status_date'] ;
	}

	//echo "<xmp>"; print_r($result); echo "</xmp>";
	$smarty->assign("historyArray", $result);
	
/*===========================================================================*/
	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	$smarty->assign("userDetArray", $userDetArray);	
	//echo "<xmp>"; print_r($userDetArray); echo "</xmp>";
/*===========================================================================*/	




if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
}

$smarty->assign('activeclass', 'paymenthistory');
$smarty->assign('mainmenu', '7');

$smarty->display(TEMPLATEDIR_ADMIN.'payment/paymenthisdetail.tpl');
unset($smarty);
?>
